package com.helen.ui.recoveryclass;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.adapter.FragmentAdapter;
import com.helen.entity.RecoveryClassEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.cache.converter.SerializableDiskConverter;
import com.zhouyou.http.cache.model.CacheMode;
import com.zhouyou.http.cache.model.CacheResult;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2018/5/9.
 * 回收电子产品分类
 */

public class ElectronicClassActivity extends BaseActivity {
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    @BindView(R.id.tl_electronic_class)
    TabLayout mTabLayout;
    private List<RecoveryClassEntity> list = new ArrayList();
    private List<RecoveryClassEntity> listTitle = new ArrayList();
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_electronic_class;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.select_electronic);
        recoveryClassListApi();
    }

    @Override
    protected void updateViews() {

    }


    private void recoveryClassListApi() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("iid", "2");
        EasyHttp.post(Constants.API_JSONAPI_RECOVERYCLASSLIST)
                .cacheMode(CacheMode.CACHEANDREMOTEDISTINCT)
                .cacheKey(Constants.API_JSONAPI_RECOVERYCLASSLIST + "_2")
                .cacheDiskConverter(new SerializableDiskConverter())
                .params(params)
                .execute(new SimpleCallBack<CacheResult<String>>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();

                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "回收分类列表失败==" + e.toString());
                        MToast.makeTextShort(ElectronicClassActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(CacheResult<String> stringCacheResult) {
                        String data = PhoneUtils.checkResponseFlag2(ElectronicClassActivity.this, stringCacheResult.data, TAG);
                        if (data != null && !data.equals("[]")) {
                            list = new Gson().fromJson(data, new TypeToken<List<RecoveryClassEntity>>() {
                            }.getType());

                            for (int i = 0; i < list.size(); i++) {
                                if (list.get(i).getParentid() == 0) {
                                    listTitle.add(list.get(i));
                                }
                            }
                            initFragment(listTitle, list);
                        } else {
                            MToast.makeTextShort(ElectronicClassActivity.this, getString(R.string.service_error)).show();
                        }


                    }
                });
    }

    private void initFragment(List<RecoveryClassEntity> listTitle, List<RecoveryClassEntity> list) {
        if (listTitle.size() > 4) {
            mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        } else {
            mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        }

        for (int i = 0; i < listTitle.size(); i++) {
            int id = listTitle.get(i).getId();
            fragments.add(BaseElectronicClassFragment.myFragment(id, list));
        }


        FragmentAdapter mFragmentAdapteradapter = new FragmentAdapter(ElectronicClassActivity.this, getSupportFragmentManager(), fragments, listTitle);
        //给ViewPager设置适配器
        viewPager.setAdapter(mFragmentAdapteradapter);
        viewPager.setOffscreenPageLimit(listTitle.size());
        //将TabLayout和ViewPager关联起来。
        mTabLayout.setupWithViewPager(viewPager);


    }
}
